package com.bjsxt.investor.service.impl;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.service.BaseService;
import com.bjsxt.common.util.DateFormatUtil;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.investor.dao.IBankDao;
import com.bjsxt.investor.dao.IGrantDao;
import com.bjsxt.investor.dao.IInvestorDao;
import com.bjsxt.investor.pojo.Bank;
import com.bjsxt.investor.pojo.Grant;
import com.bjsxt.investor.pojo.Investor;
import com.bjsxt.investor.service.IInvestorService;
@Service("investorService")
public class InvestorServiceImpl extends BaseService  implements IInvestorService
{
	@Resource
	private IGrantDao grantDao;
	@Resource
	private IInvestorDao investorDao;
	@Resource
	private IBankDao bankDao;
	
	/*************授权操作*****************/
	//查询单个的授权
	@Override
	public Grant findOneGrant(Map<String, Object> condMap)
	{
		// TODO Auto-generated method stub
		return this.grantDao.findOne(condMap);
	}

	//添加单个授权
	@Override
	public JSONObject insertOneGrant(Grant grant)
	{
		JSONObject resultJson=new JSONObject();
		int res = this.grantDao.insertOne(grant);
		if(res>0)
		{
			resultJson.put("code", 1);
			resultJson.put("info", "添加授权成功");
			
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", grant.getId());
			
			resultJson.put("date", dataJson);
		}else
		{
			resultJson.put("code", 0);
			resultJson.put("info", "添加授权失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneGrant(Grant grant)
	{
		JSONObject resultJson=new JSONObject();
		int res = this.grantDao.updateOne(grant);
		if(res>0)
		{
			resultJson.put("code", 1);
			resultJson.put("info", "更新授权成功");
			
		}else
		{
			resultJson.put("code", 0);
			resultJson.put("info", "更新授权失败");
		}
		return resultJson;
	}

	@Override
	public List<Grant> findListGrant(PageInfoUtil pageInfo,Map<String, Object> condMap)
	{
		//先判断是否分页
		if(pageInfo!=null)
		{
			//先查询总记录数
			condMap.put("pageCount", true);
			if(condMap.get("keyword")!=null){
				condMap.put("keyword", "%"+condMap.get("keyword")+"%");
			}
			List<Grant> grantLi = this.grantDao.findCondList(condMap);
			int totalRe = grantLi.get(0).getId();
			
			//准备分页查询的数据
			condMap.put("pageCount", false);
			pageInfo.setTotalRecord(totalRe);
			int currentRecord = pageInfo.getCurrRecord();
			int pageSize = pageInfo.getPageSize();
			condMap.put("currentRecord", currentRecord);
			condMap.put("pageSize", pageSize);
			//查询分页数据
			List<Grant> grantList = this.grantDao.findCondList(condMap);
			return grantList;
		}else
		{
			//直接准备不分页查询数据
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
			List<Grant> grantList = this.grantDao.findCondList(condMap);
			return grantList;
		}
		
	}
	/*************授权操作*****************/
	/*************出资方操作*****************/
	//查询单个的出资方
	@Override
	public Investor findOneInvestor(Map<String, Object> condMap)
	{
		// TODO Auto-generated method stub
		return this.investorDao.findOne(condMap);
	}

	//添加单个出资方
	@Override
	public JSONObject insertOneInvestor(Investor investor)
	{
		JSONObject resultJson=new JSONObject();
		int res = this.investorDao.insertOne(investor);
		if(res>0)
		{
			resultJson.put("code", 1);
			resultJson.put("info", "添加出资方成功");
			
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", investor.getId());
			
			resultJson.put("date", dataJson);
		}else
		{
			resultJson.put("code", 0);
			resultJson.put("info", "添加出资方失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneInvestor(Investor investor,Map<String,Object> paramMap)
	{
		JSONObject resultJson=new JSONObject();
		int res = this.investorDao.updateOne(investor);
		if(res>0)
		{
			Object object = paramMap.get("file");
			//判断投资方是否更新了logo
			if(object!=null){
				MultipartFile file = (MultipartFile)object;
				//处理文件上传
				String realPath = paramMap.get("realPath")+"";
				String originalFilename = file.getOriginalFilename();
				String absPath="/upload/investor/logo/"+DateFormatUtil.formatDate(new Date())+"/"
						+UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
				File targetFile=new File(realPath+absPath);
				boolean uploadFileResult = this.fileUtil.copy(file, targetFile);
				if(uploadFileResult)
				{
					
					//删除投资方的原logo
					String oldLogo = investor.getLogo();
					File oldFile=new File(realPath+oldLogo);
					if(oldFile.exists())
					{
						oldFile.delete();
					}
					
					//更新数据库中的图片路径
					investor.setLogo(absPath);
					int updateOne = this.investorDao.updateOne(investor);
					if(updateOne>0)
					{
						resultJson.put("code", 1);
						resultJson.put("info", "更新出资方成功");
					}else
					{
						resultJson.put("code", 0);
						resultJson.put("info", "数据库更新logo路径失败");
					}
					
				}else
				{
					resultJson.put("code", 0);
					resultJson.put("info", "上传logo失败");
				}
			}else
			{
				resultJson.put("code", 1);
				resultJson.put("info", "更新出资方成功");
			}
			
		}else
		{
			resultJson.put("code", 0);
			resultJson.put("info", "更新出资方失败");
		}
		return resultJson;
	}

	@Override
	public List<Investor> findListInvestor(PageInfoUtil pageInfo,Map<String, Object> condMap)
	{
		//先判断是否分页
		if(pageInfo!=null)
		{
			//先查询总记录数
			condMap.put("pageCount", true);
			if(condMap.get("keyword")!=null){
				condMap.put("keyword", "%"+condMap.get("keyword")+"%");
			}
			List<Investor> investorLi = this.investorDao.findCondList(condMap);
			int totalRe = investorLi.get(0).getId();
			
			//准备分页查询的数据
			condMap.put("pageCount", false);
			pageInfo.setTotalRecord(totalRe);
			int currentRecord = pageInfo.getCurrRecord();
			int pageSize = pageInfo.getPageSize();
			condMap.put("currentRecord", currentRecord);
			condMap.put("pageSize", pageSize);
			//查询分页数据
			List<Investor> investorList = this.investorDao.findCondList(condMap);
			return investorList;
		}else
		{
			//直接准备不分页查询数据
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
			List<Investor> investorList = this.investorDao.findCondList(condMap);
			return investorList;
		}
		
	}
	/*************出资方操作*****************/
	/*************银行操作*****************/
	//查询单个的银行
	@Override
	public Bank findOneBank(Map<String, Object> condMap)
	{
		// TODO Auto-generated method stub
		return this.bankDao.findOne(condMap);
	}

	//添加单个银行
	@Override
	public JSONObject insertOneBank(Bank bank)
	{
		JSONObject resultJson=new JSONObject();
		int res = this.bankDao.insertOne(bank);
		if(res>0)
		{
			resultJson.put("code", 1);
			resultJson.put("info", "添加银行成功");
			
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", bank.getId());
			
			resultJson.put("date", dataJson);
		}else
		{
			resultJson.put("code", 0);
			resultJson.put("info", "添加银行失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneBank(Bank bank)
	{
		JSONObject resultJson=new JSONObject();
		int res = this.bankDao.updateOne(bank);
		if(res>0)
		{
			resultJson.put("code", 1);
			resultJson.put("info", "更新银行成功");
			
		}else
		{
			resultJson.put("code", 0);
			resultJson.put("info", "更新银行失败");
		}
		return resultJson;
	}

	@Override
	public List<Bank> findListBank(PageInfoUtil pageInfo,Map<String, Object> condMap)
	{
		//先判断是否分页
		if(pageInfo!=null)
		{
			//先查询总记录数
			condMap.put("pageCount", true);
			if(condMap.get("keyword")!=null){
				condMap.put("keyword", "%"+condMap.get("keyword")+"%");
			}
			List<Bank> bankLi = this.bankDao.findCondList(condMap);
			int totalRe = bankLi.get(0).getId();
			
			//准备分页查询的数据
			condMap.put("pageCount", false);
			pageInfo.setTotalRecord(totalRe);
			int currentRecord = pageInfo.getCurrRecord();
			int pageSize = pageInfo.getPageSize();
			condMap.put("currentRecord", currentRecord);
			condMap.put("pageSize", pageSize);
			//查询分页数据
			List<Bank> bankList = this.bankDao.findCondList(condMap);
			return bankList;
		}else
		{
			//直接准备不分页查询数据
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
			List<Bank> bankList = this.bankDao.findCondList(condMap);
			return bankList;
		}
		
	}
	/*************银行操作*****************/
	
}
